﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Config;
using gfPage;
using gfLibrary;
using BLL;
using Model;
using DBUtility;

namespace IYAKASAPortal.PrintAll
{
    public partial class PrintAJList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                string d1 = string.Format("{0}", DateTime.Now.ToString("yyyy-MM-dd"));   //默认查询开始时间
                string d2 = string.Format("{0}", DateTime.Now.ToString("yyyy-MM-dd"));   //默认查询截止时间
                txtStart.Text = d1;
                txtEnd.Text = d2;
                BindDropDownList();
                btnSearch_Click(sender, e);
               
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SearchItem(false);
        }

        protected void GreatPager1_PageIndexChanging(object sender, gfPage.PagerEventArgs e)
        {
            GreatPager1.PageIndex = e.NewPageIndex;

            DataTable m_dtTable = GreatPager1.BindDataTablePage();
            rptList.DataSource = m_dtTable;
            rptList.DataBind();
        }


        protected void btnSearchAll_Click(object sender, EventArgs e)
        {
            SearchItem(true);
        }


        private void BindDropDownList()
        {
            CONST_DEFINE_BLL bll = new CONST_DEFINE_BLL();
            var hpzl = bll.GetModelList("CON_CHNAME='HPZL' order by CON_ORDER asc");
            drpHPZL.Items.Add(new ListItem() { Text = "全部", Value = "-1" });
            foreach (var v in hpzl)
            {
                ListItem item = new ListItem();
                item.Text = v.CON_CON;
                item.Value = v.CON_CODE;
                drpHPZL.Items.Add(item);
            }

            var clzl = bll.GetModelList("CON_CHNAME='CLZL' order by CON_ORDER asc");
            drpCLZL.Items.Add(new ListItem() { Text = "全部", Value = "-1" });
            foreach (var v in clzl)
            {
                ListItem item = new ListItem();
                item.Text = v.CON_CON;
                item.Value = v.CON_CODE;
                drpCLZL.Items.Add(item);
            }

            drpJYXL.Items.Add(new ListItem() { Text = "安检", Value = "A" });
            drpJYXL.Items.Add(new ListItem() { Text = "综检", Value = "Z" });

            drpJCCS.Items.Add(new ListItem() { Text = "全部", Value = "-1" });
            for (int i = 1; i < 11; i++)
            {
                drpJCCS.Items.Add(new ListItem() { Text = i.ToString(), Value = i.ToString()});
            }

            drpJCXH.Items.Add(new ListItem() { Text = "全部", Value = "-1" });
            for (int i = 1; i < 11; i++)
            {
                drpJCXH.Items.Add(new ListItem() { Text = i.ToString(), Value = i.ToString() });
            }

            BindJYLB(drpJYXL.SelectedValue);
        }

        private void BindJYLB(string strType)
        {
            drpJYLB.Items.Clear();
            drpJYLB.Items.Add(new ListItem() {Text = "全部",Value = "-1" });
            CONST_DEFINE_BLL bll = new CONST_DEFINE_BLL();

            string strValue = "";
            if (strType == "A" || strType == "P")
            {
                strValue = "2";
            }
            else if(strType == "Z")
            {
                strValue = "0";
            }

            var def = bll.GetModelList("CON_CHNAME='JYLB' and CON_VTYPE='" + strValue + "' order by CON_ORDER asc");

            foreach (var v in def)
            {
                ListItem item = new ListItem();
                item.Text = v.CON_CON;
                item.Value = v.CON_CODE;
                drpJYLB.Items.Add(item);
            }
        }

        private void SearchItem(bool isAll)
        {
            string strKeywords = txtKeywords.Text;
            string strSql = "select ID,JCXH,HPHM,JCLSH,VIN,JCCS,HPZL,CLXXSJ,JYLB,CLZL, (case IsUpload when '1' then '已上传' else '否' end) as Upload, (case FJXM when '' then '合格' when null then '合格' else '不合格' end) as JG from RESULT_VEHICLE_INFO where  1 =1 ";
            if (!isAll)
            {
                if (drpJYXL.SelectedValue != "")
                {
                    strSql += "  AND LEFT(JCLSH,1) = '"+drpJYXL.SelectedValue+"'";

                    if (drpJYLB.SelectedValue != "-1")
                    {
                        strSql += " AND JYLBDH LIKE '%" + drpJYLB.SelectedValue + "%'";
                    }
                }

                if (txtHPHM.Text.Trim().Length > 0)
                {
                    strSql += " AND HPHM LIKE '%" + txtHPHM.Text.Trim() + "%'";
                }
                if (txtVIN.Text.Trim().Length > 0)
                {
                    strSql += " AND VIN LIKE '%" + txtVIN.Text.Trim() + "%'";
                }

                if (drpHPZL.SelectedValue != "-1")
                {
                    strSql += " AND HPZLDH LIKE '%" + drpHPZL.SelectedValue + "%'";
                }

                if (drpCLZL.SelectedValue != "-1")
                {
                    strSql += " AND CLZLDH ='" + drpCLZL.SelectedValue + "' ";
                }

                if (drpJCCS.SelectedValue != "-1")
                {
                    strSql += " AND JCCS = '" + drpJCCS.SelectedValue + "'";
                }

                if (drpJCXH.SelectedValue != "-1")
                {
                    strSql += " AND JCXH = '" + drpJCXH.SelectedValue + "'";
                }

                if (txtStart.Text.Trim().Length > 0 && txtEnd.Text.Trim().Length > 0)
                {
                    strSql += " AND CONVERT(VARCHAR(10),CLXXSJ,120) BETWEEN CONVERT(VARCHAR(10),'" + txtStart.Text.Trim() + "',120) AND  CONVERT(VARCHAR(10),'" + txtEnd.Text.Trim() + "',120)";
                }
                else if (txtStart.Text.Trim().Length > 0)
                {
                    strSql += " AND CONVERT(VARCHAR(10),CLXXSJ,120) >= CONVERT(VARCHAR(10),'" + txtStart.Text.Trim() + "',120)";
                }
                else if (txtEnd.Text.Trim().Length > 0)
                {
                    strSql += " AND CONVERT(VARCHAR(10),CLXXSJ,120) <= CONVERT(VARCHAR(10),'" + txtEnd.Text.Trim() + "',120)";
                }
                if (drpTestResult.SelectedItem.Value == "1")
                {
                    strSql += " AND (FJXM = '' OR FJXM IS NULL)";
                }
                else if (drpTestResult.SelectedItem.Value == "2")
                {
                    strSql += " AND FJXM > ''";
                }



            }
            strSql += " order by ID desc";
            DataTable m_dtTable = new DataTable();
            GreatPager1.BindDataTablePage(ref m_dtTable, AppConfig.g_strConnectionStringSqlClient, strSql, 20);

            rptList.DataSource = m_dtTable;
            rptList.DataBind();
            m_dtTable.Dispose();
        }

        protected void btnPrintAJ_Click(object sender, EventArgs e)
        {
            string strUniqueKey = Request.Form["txtUniqueKey"];

            if ((strUniqueKey == null) || (strUniqueKey.Length == 0) || (gfValidate.Is_Integer(strUniqueKey) == false))
            {
                gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "请选择一条记录！");
                return;
            }

            RESULT_VEHICLE_INFO_BLL BLL = new RESULT_VEHICLE_INFO_BLL();
            var INFO = BLL.GetModelList("ID=" + strUniqueKey);
            string AJLSH = INFO[0].JCLSH;
            string JYXM = INFO[0].JYXM;

            string markStr = AJLSH.Substring(0, 1);

            if (markStr.ToLower().Equals("z"))
            {
               // Response.Redirect("PrintZJ_Main.aspx?UniqueKey=" + strUniqueKey);
                gfLibrary.gfAjaxJs.OpenNewUrl(this.UpdatePanel1, "PrintZJ_Main.aspx?UniqueKey=" + strUniqueKey);
            }
            else if (markStr.ToLower().Equals("p"))
            {
                if (JYXM == "X1,")
                {
                  //  Response.Redirect("PrintPF_X1_SDS.aspx?UniqueKey=" + strUniqueKey);
                    gfLibrary.gfAjaxJs.OpenNewUrl(this.UpdatePanel1, "PrintPF_X1_SDS.aspx?UniqueKey=" + strUniqueKey);
                }
                else if (JYXM == "X3,")
                {
                   // Response.Redirect("PrintPF_X3_VMAS.aspx?UniqueKey=" + strUniqueKey);
                    gfLibrary.gfAjaxJs.OpenNewUrl(this.UpdatePanel1, "PrintPF_X3_VMAS.aspx?UniqueKey=" + strUniqueKey);
                }
                else if (JYXM == "X4,")
                {
                 //   Response.Redirect("PrintPF_X4_BTG.aspx?UniqueKey=" + strUniqueKey);
                    gfLibrary.gfAjaxJs.OpenNewUrl(this.UpdatePanel1, "PrintPF_X4_BTG.aspx?UniqueKey=" + strUniqueKey);
                }
                else if (JYXM == "X6,")
                {
                   // Response.Redirect("PrintPF_X6_LZ.aspx?UniqueKey=" + strUniqueKey);
                    gfLibrary.gfAjaxJs.OpenNewUrl(this.UpdatePanel1, "PrintPF_X6_LZ.aspx?UniqueKey=" + strUniqueKey);
                }
                else if (JYXM == "X5,")
                {
                   // Response.Redirect("PrintPF_X_JZJS.aspx?UniqueKey=" + strUniqueKey);
                    gfLibrary.gfAjaxJs.OpenNewUrl(this.UpdatePanel1, "PrintPF_X_JZJS.aspx?UniqueKey=" + strUniqueKey);
                }
            }
            else
            {
                //Response.Redirect("PrintAJ.aspx?UniqueKey=" + strUniqueKey);
                gfLibrary.gfAjaxJs.OpenNewUrl(this.UpdatePanel1, "PrintAJ.aspx?UniqueKey=" + strUniqueKey);
            }

        }

        protected void drpJYXL_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strType = drpJYXL.SelectedValue;
            BindJYLB(strType);
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                string strUniqueKey = Request.Form["txtUniqueKey"];
                if ((strUniqueKey == null) || (strUniqueKey.Length == 0) || (gfValidate.Is_Integer(strUniqueKey) == false))
                {
                    gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "请选择一条记录！");
                    return;
                }

                string strDelSql = string.Format("DELETE FROM PRINT_RECORD WHERE DISPATCH_ID = '{0}'", strUniqueKey);
                string strUpdSql = string.Format("UPDATE RESULT_VEHICLE_INFO SET IsPrint = 0 WHERE ID = '{0}'", strUniqueKey);
                if (DbHelper.ExecuteSql(strDelSql))
                {
                    DbHelper.ExecuteSql(strUpdSql);
                }
                return;
                RESULT_VEHICLE_INFO_BLL bll = new RESULT_VEHICLE_INFO_BLL();
                RESULT_VEHICLE_INFO[] entitys = bll.GetModelList("ID = " + strUniqueKey);
                if (entitys.Length > 0)
                {
                    bool IsMTC = IYAKASAPortal.Statistics.StaInfoTools.GetIsMTC(entitys[0].CLZLDH);
                    string strContent = string.Empty;
                    if (IsMTC)
                    {
                        strContent = "window.open('" + string.Format("PrintAuto_MotorYQ.aspx?Key={0}", strUniqueKey) + "');";
                    }
                    else
                    {
                        strContent = "window.open('" + string.Format("PrintAuto_AJYQ.aspx?Key={0}", strUniqueKey) + "');";
                    }

                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "uuu", strContent, true);
                }
            }
            catch (Exception ex)
            {
          
            }

        }
    }
}